from common_1.Ultils import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ForumRegister:
    url=""
    driver=""
    def __init__(self):
        url="http://127.0.0.1:9580/sign-up.html"
        self.driver = ForumDriver.driver
        self.driver.get(url)

    def RegisterTest(self):
        # 对于页面元素的检查
        # 用户注册标题
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > h2")
        # 用户名及输入框
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(2) > label")
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("z_y_x")
        # 昵称及输入框
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(3) > label")
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#nickname").send_keys("cc_cjs")
        # 密码及输入框
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(4) > label")
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("785718")
        # 确认密码及输入框
        self.driver.find_element(By.CSS_SELECTOR, "#signUpForm > div > div:nth-child(5) > label")
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#passwordRepeat").send_keys("785718")

        # 同意勾选框
        self.driver.find_element(By.CSS_SELECTOR,"#signUpForm > div > div:nth-child(6) > label")
        self.driver.find_element(By.CSS_SELECTOR,"#policy").click()

        # 注册按钮
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        # 注册后页面是否成功跳转
        WebDriverWait(self.driver,15).until(
            EC.url_matches("http://127.0.0.1:9580/sign-in.html"),
            EC.presence_of_element_located((By.CSS_SELECTOR,"body > div.page.page-center"))
        )
        print("注册成功")
